package com.sitech.portal.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.sitech.ibnms.common.form.TreeNode;
import com.sitech.ibnms.common.service.IAsyncTreeService;
import com.sitech.ibnms.core.IPage;
import com.sitech.ibnms.core.PageForm;
import com.sitech.portal.system.dao.ISysRoleDao;
import com.sitech.portal.system.domain.TblSYS_ROLE;
import com.sitech.portal.system.domain.TblSYS_USER_ROLE;
import com.sitech.portal.system.service.ISysRoleService;

public class SysRoleServiceImpl implements ISysRoleService,IAsyncTreeService {

	private ISysRoleDao sysRoleDao;
	
	public ISysRoleDao getSysRoleDao() {
		return sysRoleDao;
	}

	public void setSysRoleDao(ISysRoleDao sysRoleDao) {
		this.sysRoleDao = sysRoleDao;
	}

	@Override
	public void add(TblSYS_ROLE tblSYS_ROLE) {
		// TODO Auto-generated method stub
		sysRoleDao.add(tblSYS_ROLE);
	}

	@Override
	public void addDept(TblSYS_ROLE tblSYS_ROLE) {
		// TODO Auto-generated method stub
		sysRoleDao.addDept(tblSYS_ROLE);
	}

	@Override
	public void deleteDept(String roleId) {
		// TODO Auto-generated method stub
		sysRoleDao.deleteDept(roleId);
	}

	@Override
	public void deleteRole(String roleId) {
		// TODO Auto-generated method stub
		String[] array = roleId.split(",");
		for(int i=0;i<array.length;i++){
			String id = array[i];
			if(!id.equals(""))
				sysRoleDao.deleteRole(id);
		}
	}

	@Override
	public TblSYS_ROLE findByPK(String roleID) {
		// TODO Auto-generated method stub
		return sysRoleDao.findByPK(roleID);
	}

	@Override
	public IPage findByParentRoleId(String roleId, PageForm pageForm) {
		// TODO Auto-generated method stub
		return sysRoleDao.findByParentRoleId(roleId, pageForm);
	}

	@Override
	public int findCountByParentRoleId(String roleId) {
		// TODO Auto-generated method stub
		return sysRoleDao.findCountByParentRoleId(roleId);
	}

	@Override
	public List findDeptNameList() {
		// TODO Auto-generated method stub
		return sysRoleDao.findDeptNameList();
	}

	@Override
	public IPage findForList(PageForm pageForm) {
		// TODO Auto-generated method stub
		return sysRoleDao.findForList(pageForm);
	}

	@Override
	public List findForListByDeptID() {
		// TODO Auto-generated method stub
		return sysRoleDao.findForListByDeptID();
	}

	@Override
	public List findForListByManageUnitId(String[] manageUnitId) {
		// TODO Auto-generated method stub
		return sysRoleDao.findForListByManageUnitId(manageUnitId);
	}

	@Override
	public List findForListToAll() {
		// TODO Auto-generated method stub
		return sysRoleDao.findForListToAll();
	}

	@Override
	public List findForOptions() {
		// TODO Auto-generated method stub
		return sysRoleDao.findForOptions();
	}

	@Override
	public List findForOptionsByManageUnitId(String[] manageUnitId) {
		// TODO Auto-generated method stub
		return sysRoleDao.findForOptionsByManageUnitId(manageUnitId);
	}

	@Override
	public List findForRoleListByLonginId(TblSYS_USER_ROLE obj) {
		// TODO Auto-generated method stub
		return sysRoleDao.findForRoleListByLonginId(obj);
	}

	@Override
	public TblSYS_ROLE findForTB_SYS_ROLEByRoleId(String roleID) {
		// TODO Auto-generated method stub
		return sysRoleDao.findForTB_SYS_ROLEByRoleId(roleID);
	}

    @Override
    public Boolean isAdmin(String login_id) {
        return sysRoleDao.isAdmin(login_id);  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
	public List findNoPageByParentRoleId(String roleId) {
		// TODO Auto-generated method stub
		return sysRoleDao.findNoPageByParentRoleId(roleId);
	}

	@Override
	public void update(TblSYS_ROLE tblSYS_ROLE) {
		// TODO Auto-generated method stub
		sysRoleDao.update(tblSYS_ROLE);
	}

	@Override
	public void updateDept(TblSYS_ROLE tblSYS_ROLE) {
		// TODO Auto-generated method stub
		sysRoleDao.updateDept(tblSYS_ROLE);
	}

	@Override
	public TreeNode getRootNode(String loginId, String rootId) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<TreeNode> getSubNodes(String loginId, TreeNode pnode)
			throws Exception {
		List<TreeNode> treeNodes = new ArrayList<TreeNode>();
		List roles = this.findNoPageByParentRoleId(pnode.getId()==null||"null".equals(pnode.getId())?"0":pnode.getId());
		for(Object obj : roles){
			TreeNode treeNode = new TreeNode();
			HashMap role = (HashMap)obj;
			treeNode.setId(""+role.get("ROLE_ID"));
			treeNode.setName(""+role.get("ROLE_NAME"));
			treeNode.setpId(pnode.getId());
			if("0".equals(role.get("CHILDREN").toString()))
				treeNode.setIsParent(false);
			treeNodes.add(treeNode);
		}
		return treeNodes;
	}

}
